local GoodsData = require("app.datacenter.GoodsData")

local TradeItemDetail=class("TradeItemDetail",FZutil.createUILayer)
TradeItemDetail.packBuyItems = 0
TradeItemDetail.packSellItems = 1
TradeItemDetail.recycleItems = 2
TradeItemDetail.shopItems = 3

local isPlusDown = false
local isMinusDown = false
local lastTime = 0
local longPressTime = 0
local Each_Time = 0.02
local Long_Press_Time = 0.5

function TradeItemDetail:ctor()
    lastTime = 0
	longPressTime = 0
    self.itemNum = 0
    self.imagePLs = {} --image percent line

    self:enableNodeEvents()
	local uimap=FZutil.createSceneNode("TradeUIall/TradeItemDetail")
	self.uimap=uimap
    uimap.root:setPosition(display.center)
	self:addChild(uimap.root)

    local Image_bg = uimap.Image_bg
    local Image_icon = uimap.Image_icon
    local Text_itemname = uimap.Text_itemname
    local Text_itemprice = uimap.Text_itemprice
    local Text_cost = uimap.Text_cost
    local Text_discount = uimap.Text_discount
    local Text_totalcost = uimap.Text_totalcost
    local Text_percent = uimap.Text_percent

    local function back(e)
        if e.name=="ended" then
           self:removeFromParent()
        end
    end
    self:onTouch(back)

    uimap.Slider_num:addEventListener(function (target)
        local sliderParency = string.format("%d",uimap.Slider_num:getPercent())
        self.itemNum = math.floor(sliderParency*self.itemTotalNum*0.01)
        self:updateItemDetail()
    end)

    self:onUpdate(function(delta)
        lastTime=lastTime+delta
        if longPressTime <= Long_Press_Time then longPressTime = longPressTime+delta end     
        if lastTime>Each_Time and longPressTime>Long_Press_Time then
        	lastTime=0
        	if isPlusDown then
        		self:plusUpdateItemDetail()
            elseif isMinusDown then
                self:minusUpdateItemDetail()
        	end      
        end
    end)

    uimap.Button_del:onTouch(function (e)
        if e.name == "began" then
			self:onBeganEvent(function()
				isMinusDown = true
				self:minusUpdateItemDetail()
			end)
		elseif e.name == "ended" or e.name == "cancelled" then
			isMinusDown = false
		end
    end)

    uimap.Button_add:onTouch(function (e)
        if e.name == "began" then
			self:onBeganEvent(function()
				isPlusDown = true
				self:plusUpdateItemDetail()
			end)
		elseif e.name == "ended" or e.name == "cancelled" then
			isPlusDown = false
		end
    end)

    local function confirm(e)
        if e.name=="ended" then
            if self.itemtype == TradeItemDetail.shopItems then
                --买入商店货物
                DyTransportPack:purchaseGoods(self.itemid,self.itemNum)
            elseif self.itemtype == TradeItemDetail.packBuyItems then
                --马车背包取消买入
                DyTransportPack:undoPurchase(self.packIndex,self.itemNum)
            elseif self.itemtype == TradeItemDetail.packSellItems then
                --马车背包卖出
                DyTransportPack:sellGoods(self.packIndex,self.itemNum)
            elseif self.itemtype == TradeItemDetail.recycleItems then
                --从回收车取出
                DyTransportPack:undoSell(self.itemid,self.itemNum)
            end        
            FzEventCenter:DispichEvent(FzEvent.UpdateTradeingInfo)
            self:removeFromParent()
        end
    end

    local Button_cancel = uimap.Button_cancel
    local Button_buyconfirm = uimap.Button_buyconfirm
    Button_cancel:onTouch(back)
    Button_buyconfirm:onTouch(confirm)
  
end
--param1 type
function TradeItemDetail:setItemInfo(itemtype,param2,param3)
    self.itemtype = itemtype
    local function getBuyMax(moneytype,price)
        if moneytype==1 then
            return math.floor(RoleData.player_info.gold/price)
        elseif moneytype==2 then
            return math.floor(RoleData.player_info.diamond/price)
        end
    end
    if itemtype == TradeItemDetail.shopItems then
        --param2 id param3 count
        self.itemid = param2
        local item = GoodsData.new(param2,param3)
        local itemcount = param3==-1 and item.overlying or param3
        local buymaxcount = getBuyMax(item.moneytype,item:getBuyPrice())
        self.itemTotalNum = math.min(itemcount,buymaxcount)
        self.price = item:getBuyPrice()
    elseif itemtype == TradeItemDetail.packBuyItems then
        self.packIndex = param2
        self.itemTotalNum = param3
        self.price = 0
    elseif itemtype == TradeItemDetail.packSellItems then
        self.packIndex = param2
        self.itemTotalNum = param3
        local item = DyTransportPack:getGoodsData(self.packIndex)
        self.price = item:getSellPrice()
    elseif itemtype == TradeItemDetail.recycleItems then
        self.itemid = param2
        self.itemTotalNum = param3
        self.price = 0
    end
    self:updateItemDetail()
end

function TradeItemDetail:minusUpdateItemDetail()
    if self.itemNum >0 then
        self.itemNum = self.itemNum - 1
        self:updateItemDetail()
    end
end

function TradeItemDetail:plusUpdateItemDetail()
    if self.itemNum < self.itemTotalNum then
        self.itemNum = self.itemNum + 1
        self:updateItemDetail()
    end
end

function TradeItemDetail:updateItemDetail()
    self.uimap.Text_cost:setString(self.price)
    if self.price == 0 then
        self.uimap.Text_cost:setVisible(false)
    end
    self.uimap.Text_percent_N:setString(self.itemNum.."/"..self.itemTotalNum)
    self.uimap.Slider_num:setPercent(self.itemNum/self.itemTotalNum*100)
    self.uimap.Text_totalcost:setString(self.itemNum*self.price)
    --存储量 格子
    for i=1,10 do
        local capacityState = (self.itemNum/self.itemTotalNum*10>=i) and 1 or 2
        local imagePath = string.format("UI/TradeUI/percent_line%02d.png",capacityState)
        local imagePL = self.imagePLs[i]
        if imagePL then
            imagePL:loadTexture(imagePath)
        else
            imagePL = ccui.ImageView:create(imagePath)
            imagePL:setPosition(self.uimap.Text_2_N:getPositionX()+60+i*20,self.uimap.Text_2_N:getPositionY())
            self.uimap.Image_bg:addChild(imagePL)
            self.imagePLs[i] = imagePL
        end
    end
end

--开始长按数据重置
function TradeItemDetail:onBeganEvent(callfun)
    lastTime=0
    longPressTime=0
    callfun()
end

function TradeItemDetail:onEnter()

end

function TradeItemDetail:onExit()

end

return TradeItemDetail